package EA;
import EA.Framework;

/** Abstract Genome class for any EA. This class simulates the topdown inner
call found in the (GREAT) programming language BETA. Instead of doing a 
super.somemethod(), the superclass' methods contain a call to an inner method.
That way code reuse is enforced in a FAR better way than java's method 
overriding strategy.
*/
public class Genome implements Cloneable
{
  
  public Framework framework;
  /** The last calculated fitness. Updated by Genome_getFitness_inner(). */
  public double fitness;
  /** True if the fitness have been calculated for this genome. */
  public boolean fitnesscalculated;

  public Genome(Framework frame)
    {
      framework = frame;
      fitnesscalculated = false;
    }

  /** Calculate fitness of genome. <br>
      <b>Note:</b> The method remembers the last calculated value, so it 
      doesn't have to recalculate again. This can give trouble for algorithms
      in dynamic landscapes, because the fitness change over time. To solve
      this either set fitnesscalculated = false or call recalcFitness. 
  */
  public double getFitness()
    {
      if (!fitnesscalculated)
	{
	  Genome_getFitness_inner();
	  fitnesscalculated = true;
	}
      return fitness;
    }

  /** Set the fitness of this genome.
      @param thefitness The new fitness.
  */
  public void setFitness(double thefitness)
    {
      fitnesscalculated = true;
      fitness = thefitness;
    }

  /** BETA style inner call for getFitness().<br>
      Override this method when subclassing.
  */
  public void Genome_getFitness_inner()
    {
    }

  /** Recalculate the fitness next time getFitness is invoked. 
      @see EA.Genome#getFitness
  */
  public void recalcFitness()
    {
      fitnesscalculated = false;
    }

  /** Mutate genome. 
      @param p_m Probability of mutation.
*/
  public void mutate(double p_m)
    {
      fitnesscalculated = false;
      Genome_mutate_inner(p_m);
    }

  /** BETA style inner call from mutate.<br>
      Override this method when subclassing this class.
      @param p_m Probability of mutation.
  */
  public void Genome_mutate_inner(double p_m)
    {
    }

  /** Perform a crossover between genome1 and genome2. */
  public static Genome crossover(Genome genome1, Genome genome2)
    {
    return null;
    }

    /** You must override this if you want to clone the genome. 
	@return null;
    */
    public Object clone()
    {
	return null;
    }
}
